package dt.inlee.common.util;

import java.util.ArrayList;
import java.util.List;

public class SmilUtil {
	public static class GroupName {
		private String txt;

		private String pic;

		private String aud;

		private int playduration = 60;

		public int getPlayduration() {
			return playduration;
		}

		public void setPlayduration(int playduration) {
			this.playduration = playduration;
		}

		public String getAud() {
			return aud;
		}

		public void setAud(String aud) {
			this.aud = aud;
		}

		public String getPic() {
			return pic;
		}

		public void setPic(String pic) {
			this.pic = pic;
		}

		public String getTxt() {
			return txt;
		}

		public void setTxt(String txt) {
			this.txt = txt;
		}
	}

	public static String fileName2Smil(List<GroupName> list) {
		return fileName2Smil(list, 160, 120);
	}

	public static String fileName2Smil(List<GroupName> list, int picWidth,
			int picHeight) {
		if (list == null || list.size() <= 0)
			return null;
		StringBuffer smil = new StringBuffer();
		smil.append("<smil>\n");
		smil.append("<head>\n");
		smil.append("<layout>\n");
		smil.append("<root-layout width=\"160\" height=\"140\"/>\n");
		smil.append("<region id=\"Image\" width=\"" + picWidth + "\" height=\""
				+ picHeight + "\" left =\"0\" top=\"0\" />\n");
		smil
				.append("<region id=\"Text\" width=\"160\" height=\"20\" left =\"0\" top=\""
						+ picHeight + "\" />\n");
		smil.append("</layout>\n");
		smil.append("</head>\n");
		smil.append("<body>\n");
		for (GroupName gn : list) {
			if (gn == null)
				continue;
			if (gn.getTxt() == null && gn.getPic() == null
					&& gn.getAud() == null)
				continue;
			smil.append("<par dur=\"" + gn.getPlayduration() + "s\">\n");
			if (gn.getPic() != null) {
				smil.append("<img src=\"" + gn.getPic()
						+ "\" region=\"Image\"/>\n");
			}
			if (gn.getTxt() != null) {
				smil.append("<text src=\"" + gn.getTxt()
						+ "\" region=\"Text\"/>\n");
			}
			if (gn.getAud() != null) {
				smil.append("<audio src=\"" + gn.getAud() + "\"/>\n");
			}
			smil.append("</par>\n");
		}
		smil.append("</body>\n");
		smil.append("</smil>");
		return smil.toString();
	}

	public static byte[] fileName2SmilByte(ArrayList<GroupName> list) {
		if (list == null || list.size() <= 0)
			return null;
		return fileName2Smil(list).getBytes();
	}

	public static void main(String[] arges) {
		ArrayList<GroupName> list = new ArrayList<GroupName>(9);
		GroupName gn1 = new GroupName();
		gn1.setPic("young LOGO.jpg");
		gn1.setTxt("1.txt");
		gn1.setPlayduration(30);
		list.add(gn1);
		gn1 = new GroupName();
		gn1.setPic("2.jpg");
		gn1.setTxt("2.txt");
		gn1.setPlayduration(25);
		list.add(gn1);
		gn1 = new GroupName();
		gn1.setPic("3.jpg");
		gn1.setTxt("3.txt");
		gn1.setPlayduration(57);
		list.add(gn1);
		gn1 = new GroupName();
		gn1.setPic("4.jpg");
		gn1.setTxt("4.txt");
		gn1.setPlayduration(22);
		list.add(gn1);
		gn1 = new GroupName();
		gn1.setPic("5.jpg");
		gn1.setTxt("5.txt");
		gn1.setPlayduration(34);
		list.add(gn1);
		gn1 = new GroupName();
		gn1.setPic("6.jpg");
		gn1.setTxt("6.txt");
		gn1.setPlayduration(36);
		list.add(gn1);
		gn1 = new GroupName();
		gn1.setPic("7.jpg");
		gn1.setTxt("7.txt");
		gn1.setPlayduration(40);
		list.add(gn1);
		gn1 = new GroupName();
		gn1.setPic("8.jpg");
		gn1.setTxt("8.txt");
		list.add(gn1);
		System.out.println(fileName2Smil(list));
	}
}
